﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Common;
using Mimoza.DBLayer.Transport;
using Mimoza.Transport;

namespace Mimoza.DBLayer.Transport.Client
{
    public class PoliciesRepository :
        ClientRepository<PolicyRecord>,
        IDBPolicyRepository
    {
        public PoliciesRepository()
        {
            PoliciesRepositoryTranferRequest.RegisterInTransport();
            RepositoryReply<PolicyRecord>.RegisterInTransport();
        }

        public PolicyRecord GetPolicy(Guid ID)
        {
            return MakeObjectRequest(
                    PoliciesRepositoryCustomRequest.CustomOperationType.GetPolicy, ID);
        }

        public IEnumerable<PolicyRecord> GetPolicies(Guid policyID)
        {
            return MakeCollectionRequest<PolicyRecord>(
                    PoliciesRepositoryCustomRequest.CustomOperationType.GetPolicies,
                    policyID);
        }

        override public void Insert(PolicyRecord policy)
        {
            policy = MakeObjectRequest(new PoliciesRepositoryTranferRequest(Operation.Insert, policy));
            base.Insert(policy);
        }

        override public void Update(PolicyRecord policy)
        {
            policy = MakeObjectRequest(new PoliciesRepositoryTranferRequest(Operation.Update, policy));
            base.Update(policy);
        }

        override public void Delete(PolicyRecord policy)
        {
            policy = MakeObjectRequest(new PoliciesRepositoryTranferRequest(Operation.Delete, policy));
            base.Delete(policy);
        }

        PolicyRecord MakeObjectRequest(PoliciesRepositoryCustomRequest.CustomOperationType oper,
            Guid argument)
        {
            PoliciesRepositoryCustomRequest customReq = new PoliciesRepositoryCustomRequest(oper, argument.ToString());
            return MakeObjectRequest(new PoliciesRepositoryTranferRequest(customReq));
        }

        PolicyRecord MakeObjectRequest(PoliciesRepositoryCustomRequest.CustomOperationType oper,
            String argument)
        {
            PoliciesRepositoryCustomRequest customReq = new PoliciesRepositoryCustomRequest(oper, argument);
            return MakeObjectRequest(new PoliciesRepositoryTranferRequest(customReq));
        }

        ClientCollectionEnumerable<TCollectionRecord> MakeCollectionRequest<TCollectionRecord>(PoliciesRepositoryCustomRequest.CustomOperationType oper,
            Guid argument)
            where TCollectionRecord : class, new()
        {
            PoliciesRepositoryCustomRequest customReq = new PoliciesRepositoryCustomRequest(oper, argument.ToString());
            return MakeCollectionRequest<TCollectionRecord>(new PoliciesRepositoryTranferRequest(customReq));
        }
    }
}
